package com.wj.demo;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class GenEntityMysql {

    private String packageOutPath = "自动创建实体";//指定实体生成所在包的路径
    private String authorName = "wj";//作者名字
    private String tablename = "rcp_unpaid_summary";//表名
    private String[] colnames; // 列名数组
    private String[] schemaNames;//
    private String[] colTypes; //列名类型数组
    private int[] colSizes; //列名大小数组
    private Map comment;	 //字段注释
    private boolean f_util = true; // 是否需要导入包java.util.*
    private boolean f_sql = false; // 是否需要导入包java.sql.*

    //数据库连接
    String databaseName = "property_system";
    //private static final String URL ="jdbc:mysql://192.168.34.10:3306/data-archives-management-platform-dev?autoReconnect=true&failOverReadOnly=false&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
    //private static final String URL ="jdbc:mysql://localhost:3306/cloud-demo?autoReconnect=true&failOverReadOnly=false&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
    private static final String URL = "jdbc:mysql://192.168.34.10:3306/property_system?autoReconnect=true&failOverReadOnly=false&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
    private static final String NAME = "developer";
    private static final String PASS = "Dev@123456";
    //private static final String NAME = "root";
    //private static final String PASS = "123456";
    private static final String DRIVER ="com.mysql.cj.jdbc.Driver";

    /*
     * 构造函数
     */
    public GenEntityMysql(){
        //创建连接
        Connection con = null;
        //查要生成实体类的表
        String sql = "select * from " + tablename;
        PreparedStatement pStemt = null;
        PreparedStatement pStemt2 = null;
        try {
            try {
                Class.forName(DRIVER);
            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();
            }
            con = DriverManager.getConnection(URL,NAME,PASS);
            pStemt = con.prepareStatement(sql);
            ResultSetMetaData rsmd = pStemt.getMetaData();
            int size = rsmd.getColumnCount();	//统计列
            colnames = new String[size];
            colTypes = new String[size];
            colSizes = new int[size];
            schemaNames = new String[size];
            for (int i = 0; i < size; i++) {
                colnames[i] = rsmd.getColumnName(i + 1);
                colTypes[i] = rsmd.getColumnTypeName(i + 1);
                schemaNames[i] = rsmd.getSchemaName(i + 1);

                if(colTypes[i].equalsIgnoreCase("datetime")){
                    f_util = true;
                }
                if(colTypes[i].equalsIgnoreCase("image") || colTypes[i].equalsIgnoreCase("text")){
                    f_sql = true;
                }
                colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
            }

            ResultSet primaryKeys = con.getMetaData().getPrimaryKeys(null, null, tablename);
            String PrimaryKeyColumnName = "";
            if (!primaryKeys.isAfterLast()) {
                primaryKeys.next();
                PrimaryKeyColumnName = primaryKeys.getString("COLUMN_NAME");
                System.out.println(primaryKeys.getString("TABLE_NAME") + " " +primaryKeys.getString("COLUMN_NAME"));
            }
            //获取数据库字段注释
            String commentSql = " show full fields from " + tablename;
            comment = new HashMap();
            pStemt2 = con.prepareStatement(commentSql);
            ResultSet rs2 = pStemt2.executeQuery();
            while(rs2.next()){
                String comm  = rs2.getString("Comment");
                if(null != comm && !comm.equals("")){
                    comment.put(rs2.getString("Field"), comm);
                }else{
                    comment.put(rs2.getString("Field"), rs2.getString("Field"));
                }
            }
            System.out.println(comment);
            String content = parse(colnames,colTypes,colSizes, PrimaryKeyColumnName);

            try {
                File directory = new File("");
                String outputPath = directory.getAbsolutePath()+ "\\src\\"+this.packageOutPath.replace(".", "\\") + "\\";
                File file = new File(outputPath);
                file.mkdirs();
                FileWriter fw = new FileWriter(outputPath + initcap(colnameToHump(tablename)) + "Entity.java");
                PrintWriter pw = new PrintWriter(fw);
                pw.println(content);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 功能：生成实体类主体代码
     * @param colnames
     * @param colTypes
     * @param colSizes
     * @param primaryKeyColumnName 主键名
     * @return
     */
    private String parse(String[] colnames, String[] colTypes, int[] colSizes, String primaryKeyColumnName) {
        StringBuffer sb = new StringBuffer();
        sb.append("package " + this.packageOutPath + ";\r\n");
        sb.append("\r\n");
        //判断是否导入工具包
        if(f_util){
            sb.append("import java.util.Date;\r\n");
        }
        if(f_sql){
            sb.append("import java.sql.*;\r\n");
        }
        //注释部分
        sb.append("\r\n");
        sb.append("/**\r\n");
        sb.append(" * "+tablename+" 实体类\r\n");
        sb.append(" * "+new Date()+" "+this.authorName + "\r\n");
        sb.append(" */ \r\n");
        //实体部分
        sb.append("\r\n");
        sb.append("@Entity\n" +
                "@Table(name = \""+tablename+"\", schema = \""+databaseName+"\", catalog = \"\")\r\n");
        sb.append("public class " + initcap(colnameToHump(tablename)) + "Entity {\r\n");
        processAllAttrs(sb);//属性
        processAllMethod(sb, primaryKeyColumnName);//get set方法
        sb.append("}\r\n");

        return sb.toString();
    }

    /**
     * 功能：生成所有属性
     * @param sb
     */
    private void processAllAttrs(StringBuffer sb) {

        for (int i = 0; i < colnames.length; i++) {
            sb.append("\t\r\n");
            sb.append("\t/**\r\n");
            sb.append("\t* "+ comment.get(colnames[i]) + "\r\n");
            sb.append("\t*/ \r\n");
            sb.append("\t@ApiModelProperty(value = \""+comment.get(colnames[i])+"\")\r\n");
            sb.append("\tprivate " + sqlType2JavaType(colTypes[i]) + " " + colnameToHump(colnames[i]) + ";\r\n");
        }

    }

    /**
     * 功能：生成getter,setter方法
     * @param sb
     */
    private void processAllMethod(StringBuffer sb, String primaryKeyColumnName) {

        for (int i = 0; i < colnames.length; i++) {
            String column = "";
            sb.append("\r\n");
            String columnName = colnames[i];
            if (columnName.equals(primaryKeyColumnName)){
                sb.append("\t@Id");
            }
            sb.append("\r\n");
            sb.append("\t@Column(name=\""+colnames[i]+"\")\r\n");
            sb.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get" + initcap(colnameToHump(colnames[i])) + "(){\r\n");
            sb.append("\t\treturn " + colnameToHump(colnames[i]) + ";\r\n");
            sb.append("\t}\r\n");
            sb.append("\r\n");
            sb.append("\tpublic void set" + initcap(colnameToHump(colnames[i])) + "(" + sqlType2JavaType(colTypes[i]) + " " +
                    colnameToHump(colnames[i]) + "){\r\n");
            sb.append("\t\tthis." + colnameToHump(colnames[i]) + " = " + colnameToHump(colnames[i]) + ";\r\n");
            sb.append("\t}\r\n");

        }

    }
    /**
     * 功能：将数据库的字段改成驼峰命名的方式
     */
    public String colnameToHump(String colnames) {
        String[] tf = colnames.split("_");
        String newColName = "";
        for(int i=0; i<tf.length;i++) {
            if (i != 0){
                tf[i] = initcap(tf[i]);
            }
            newColName = newColName + tf[i];
        }
        return newColName;
    }

    /**
     * 功能：将输入字符串的首字母改成大写
     * @param str
     * @return
     */
    private String initcap(String str) {

        char[] ch = str.toCharArray();
        if(ch[0] >= 'a' && ch[0] <= 'z'){
            ch[0] = (char)(ch[0] - 32);
        }

        return new String(ch);
    }

    /**
     * 功能：获得列的数据类型
     * @param sqlType
     * @return
     */
    private String sqlType2JavaType(String sqlType) {

        if(sqlType.equalsIgnoreCase("bit")){
            return "Boolean";
        }else if(sqlType.equalsIgnoreCase("tinyint")){
            return "Byte";
        }else if(sqlType.equalsIgnoreCase("smallint")){
            return "Integer";
        }else if(sqlType.equalsIgnoreCase("int")){
            return "Integer";
        }else if(sqlType.equalsIgnoreCase("bigint")){
            return "Long";
        }else if(sqlType.equalsIgnoreCase("float")){
            return "Float";
        }else if(sqlType.equalsIgnoreCase("numeric")
                || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money")
                || sqlType.equalsIgnoreCase("smallmoney")){
            return "Double";
        }else if(sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char")
                || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar")
                || sqlType.equalsIgnoreCase("text")){
            return "String";
        }else if(sqlType.equalsIgnoreCase("datetime")
                || sqlType.equalsIgnoreCase("date")){
            return "Date";
        }else if(sqlType.equalsIgnoreCase("image")){
            return "Blod";
        }else if(sqlType.equalsIgnoreCase("decimal")){
            return "BigDecimal";
        }

        return null;
    }

    /**
     * 出口
     * @param args
     */
    public static void main(String[] args) {

        new GenEntityMysql();
    }

}